import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: isMobile() ? '/start' : '/webstart'
  },
  {
    path: '/main',
    name: 'main',
    redirect: '/home',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/main.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName: "home页面" */ '@/views/home/home.vue')
      },
      {
        path: '/borrow',
        name: 'borrow',
        component: () => import(/* webpackChunkName: "borrow页面" */ '@/views/borrow/borrow.vue')
      },
      {
        path: '/select',
        name: 'select',
        component: () => import(/* webpackChunkName: "select页面" */ '@/views/select/select.vue')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import(/* webpackChunkName: "user页面" */ '@/views/user/user.vue')
      }
    ]
  },
  {
    path: '/bookdetail',
    name: 'bookdetail',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/home/bookdetail.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/login.vue')
  },
  {
    path: '/start',
    name: 'start',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/start.vue')
  },
  {
    path: '/webstart',
    name: 'webstart',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/weblogin.vue')
  },
  {
    path: '/allbook',
    name: 'allbook',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/home/allbook.vue')
  },
  {
    path: '/editperson',
    name: 'editperson',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/user/editperson.vue')
  },
  {
    path: '/rebook',
    name: 'rebook',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/user/rebook.vue')
  },
  {
    path: '/addbook',
    name: 'addbook',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/home/addbook.vue')
  },
  {
    path: '/checkadd',
    name: 'checkadd',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/user/checkadd.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/404.vue')
  },
  {
    path: '/backend',
    name: 'backend',
    redirect: 'log',
    component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/backend.vue'),
    meta: {
      requireAuth: true
    },
    children: [
      {
        path: '/log',
        name: 'log',
        component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/page/log.vue'),
        meta: {
          requireAuth: true,
          menuname: '日志管理'
        }
      },
      {
        path: '/bookmanage',
        name: 'bookmanage',
        component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/page/bookmanage.vue'),
        meta: {
          requireAuth: true,
          menuname: '书籍管理'
        }
      },
      {
        path: '/hotbook',
        name: 'hotbook',
        component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/page/hotbook.vue'),
        meta: {
          requireAuth: true,
          menuname: '热门书籍'
        }
      },
      {
        path: '/combook',
        name: 'combook',
        component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/page/combook.vue'),
        meta: {
          requireAuth: true,
          menuname: '推荐书籍'
        }
      },
      {
        path: '/bookreq',
        name: 'bookreq',
        component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/page/bookreq.vue'),
        meta: {
          requireAuth: true,
          menuname: '书籍审核'
        }
      },
      {
        path: '/users',
        name: 'users',
        component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/page/users.vue'),
        meta: {
          requireAuth: true,
          menuname: '用户管理'
        }
      },
      {
        path: '/webhome',
        name: 'webhome',
        component: () => import(/* webpackChunkName: "home页面" */ '@/views/web/page/webhome.vue'),
        meta: {
          requireAuth: true
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (to.meta.requireAuth) {
    if (store.state.token) {
      next()
    } else {
      next({ path: '/404' })
    }
  } else {
    next()
  }
})

router.afterEach(() => {
  NProgress.done()
})

function isMobile () {
  const flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  )
  return flag
}
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
